<?php

namespace lib;

class AES {

    /**
     * @var string
     */
    private $key;

    /**
     * @var string
     */
    private $iv;

    /**
     * @var string
     */
    private $method;

    public function __construct(string $key, string $iv, string $method = "AES-128-CBC") {
        $this->key = $key;
        $this->iv = $iv;
        $this->method = $method;
    }

    // 加密
    public function encrypt(string $data): string {
        return base64_encode(openssl_encrypt($data, $this->method, $this->key, 0, $this->iv));
    }

    // 解密
    public function decrypt(string $data) {
        return openssl_decrypt(base64_decode($data), $this->method, $this->key, 0, $this->iv);
    }


}